package com.collect.zambia.zhiScene.zhiWidget.zhiFilterCustomerService;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiFilterDaily.ZHIFilterConditionAdjuster;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRFilterEntity;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeMobileInput;
import com.collect.zambia.zhiUtils.StringUtil;

import java.util.ArrayList;

@SuppressLint("NonConstantResourceId")
public class ZHIFilterCustomerService extends RelativeLayout {

    private final Context mContext;

    private RecyclerView rc_order_type_list;
    private RecyclerView rc_call_result_list;
    private RecyclerView rc_paid_status_list;

    private ZHIRangeMobileInput rds_user_mobile;

    private ZHIFilterConditionAdjuster mZHIFilterOrderTypeAdjuster;
    private ZHIFilterConditionAdjuster mZHIFilterCallResultAdjuster;
    private ZHIFilterConditionAdjuster mZHIFilterPaidStatusAdjuster;

    private ArrayList<ZHIRFilterEntity> mOrderTypeFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mCallResultFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mPaidStatusFilterArrayList;

    private ZHIFilterDailyClickListener mZHIFilterDailyClickListener;

    public ZHIFilterCustomerService(Context context) {
        this(context, null);
    }

    public ZHIFilterCustomerService(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIFilterCustomerService(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.mContext = context;

        initView();
    }

    private void initView() {
        try {
            View mView = View.inflate(mContext, R.layout.view_filter_customer_service, this);

            rc_order_type_list = mView.findViewById(R.id.rc_order_type_list);
            rc_call_result_list = mView.findViewById(R.id.rc_call_result_list);
            rc_paid_status_list = mView.findViewById(R.id.rc_paid_status_list);
            rds_user_mobile = mView.findViewById(R.id.rds_user_mobile);

            TextView tv_reset = mView.findViewById(R.id.tv_reset);
            tv_reset.setOnClickListener(v -> {
                for (ZHIRFilterEntity zhirFilterEntity1 : mOrderTypeFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equals(mContext.getResources().getString(R.string.today_all_orders)));
                }
                mZHIFilterOrderTypeAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mCallResultFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equals("-1"));
                }
                mZHIFilterCallResultAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mPaidStatusFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equals(mContext.getResources().getString(R.string.today_all_orders)));
                }
                mZHIFilterPaidStatusAdjuster.notifyDataSetChanged();

                rds_user_mobile.reset();
            });

            RelativeLayout rl_close_bg = mView.findViewById(R.id.rl_close_bg);
            rl_close_bg.setOnClickListener(v -> {
                if (mZHIFilterDailyClickListener != null) {
                    mZHIFilterDailyClickListener.onCloseClick();
                }
            });

            TextView tv_submit = mView.findViewById(R.id.tv_submit);
            tv_submit.setOnClickListener(v -> {
                if (mZHIFilterDailyClickListener != null) {
                    mZHIFilterDailyClickListener.onConfirmClick();
                }
            });

            ////////////////////////////////////
            initOrderTypeAdjuster();

            ////////////////////////////////////
            initCallResultAdjuster();

            ////////////////////////////////////
            initPaidStatusAdjuster();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initOrderTypeAdjuster() {
        String[] datas = new String[]{
                mContext.getResources().getString(R.string.today_all_orders),
                mContext.getResources().getString(R.string.order_type_d0),
                mContext.getResources().getString(R.string.order_type_d1)};

        String[] dataValues = new String[]{
                "",
                "TODAY",
                "EARLY_ONE_DAY"};

        mOrderTypeFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(dataValues[i]);
            zhirFilterEntity.setChecked(i == 0);
            mOrderTypeFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterOrderTypeAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterOrderTypeAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mOrderTypeFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());

                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterOrderTypeAdjuster.notifyDataSetChanged();
            }
        });
        rc_order_type_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_order_type_list.setAdapter(mZHIFilterOrderTypeAdjuster);
        mZHIFilterOrderTypeAdjuster.setNewData(mOrderTypeFilterArrayList);
    }

    private void initCallResultAdjuster() {
        String[] datas = new String[]{
                mContext.getResources().getString(R.string.call_result_ptp),
                mContext.getResources().getString(R.string.call_result_rnr),
                mContext.getResources().getString(R.string.call_result_fake),
                mContext.getResources().getString(R.string.call_result_switched_off),
                mContext.getResources().getString(R.string.call_result_innvalid_num),
                mContext.getResources().getString(R.string.call_result_out_of_coverage),
                mContext.getResources().getString(R.string.call_result_msg_sent),
                mContext.getResources().getString(R.string.call_result_paid),
                mContext.getResources().getString(R.string.call_result_connected)};

        mCallResultFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(datas[i]);
            zhirFilterEntity.setChecked(false);
            mCallResultFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterCallResultAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterCallResultAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mCallResultFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());
                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterCallResultAdjuster.notifyDataSetChanged();
            }
        });
        rc_call_result_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_call_result_list.setAdapter(mZHIFilterCallResultAdjuster);
        mZHIFilterCallResultAdjuster.setNewData(mCallResultFilterArrayList);
    }

    private void initPaidStatusAdjuster() {
        String[] datas = new String[]{
                mContext.getResources().getString(R.string.today_all_orders),
                mContext.getResources().getString(R.string.repaid_status_settled),
                mContext.getResources().getString(R.string.repaid_status_unsettle)};

        String[] dataValues = new String[]{
                "",
                "YES",
                "NO"};

        mPaidStatusFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(dataValues[i]);
            zhirFilterEntity.setChecked(i == 0);
            mPaidStatusFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterPaidStatusAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterPaidStatusAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mPaidStatusFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());

                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterPaidStatusAdjuster.notifyDataSetChanged();
            }
        });
        rc_paid_status_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_paid_status_list.setAdapter(mZHIFilterPaidStatusAdjuster);
        mZHIFilterPaidStatusAdjuster.setNewData(mPaidStatusFilterArrayList);
    }

    public void setZHIFilterDailyClickListener(ZHIFilterDailyClickListener listener) {
        this.mZHIFilterDailyClickListener = listener;
    }

    public String getOrderType() {

        String tResult = "";
        if (mOrderTypeFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mOrderTypeFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }

        return tResult;
    }

    public String getOrderTypeD0() {
        return "TODAY";
    }

    public String getOrderTypeD1() {
        return "EARLY_ONE_DAY";
    }

    public String getCallResult() {
        String tResult = "";
        if (mCallResultFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mCallResultFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }
        return tResult;
    }

    public String getPaidStatus() {
        String tResult = "";
        if (mPaidStatusFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mPaidStatusFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }
        return tResult;
    }

    public String getUserMobile() {
        return StringUtil.safeString(rds_user_mobile.getMobile());
    }
}
